import { logoutPost } from '@/api'
import type { OInfoGet, OInfoGetUser, OMenusGetItem } from '@/typed/response'
import { OInfoGetUserValue } from '@/typed/response.value'
import { defineStore } from 'pinia'

interface UserStoreProps {
	permissions: string[];
	roles: string[];
	user: OInfoGetUser;
	menu: OMenusGetItem[];
}

export const useUserStore = defineStore('user', {
	state: (): UserStoreProps => {
		return {
			permissions: [],
			roles: [],
			user: {
				...OInfoGetUserValue
			},
			menu: []
		}
	},
	actions: {
		setUserInfo(userInfo: OInfoGet) {
			this.permissions = userInfo.permissions
			this.roles = userInfo.roles
			this.user = userInfo.user
		},
		setMenu(menu: OMenusGetItem[]) {
			this.menu = menu
		},
		async logout() {
			await logoutPost()
		}
	}
})
